package com.hjjt.cameralib

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Environment
import android.util.Log
import android.view.SurfaceView
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import com.hjjt.baselib.HJBaseAdapter
import com.hjjt.cameralib.cameraengin.CameraEngine
import com.hjjt.cameralib.cameraengin.CameraFactory
import com.hjjt.cameralib.const.CameraConst
import com.hjjt.cameralib.fragment.GLSurfaceFragment
import com.hjjt.cameralib.fragment.PreviewFragment
import com.hjjt.cameralib.listener.LiveCheckListener
import com.hjjt.cameralib.opengl.GLFrameSurface
import com.hjjt.cameralib.utils.SoLoadUtil
import com.hjjt.cameralib.utils.TinkerLoadLibrary
import com.hjjt.cameraplugin.CameraEngineConfig
import com.hjjt.cameraplugin.ImageData
import com.hjjt.filelib.FileUtil
import com.hjjt.logger.logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.lang.ref.WeakReference

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.cameralib
 * @ClassName:      CameraInstance
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/8/15 8:52
 */
class CameraInstance private constructor() {
    var cameraEngine: CameraEngine? = null
    var isInit = false
    var msgCallBack: ((code: Int, msg: String, data: ImageData?) -> Unit)? = null
    var showPreview = false
    var applicationContext: Context? = null

    companion object {
        val instance by lazy {
            CameraInstance()
        }
    }

    fun initCallBack(callBack: (code: Int, msg: String, data: ImageData?) -> Unit) {
        msgCallBack = callBack
    }

    fun init(
        angle: CameraEngineConfig.Angle,
        type: CameraEngineConfig.CameraEngineType,
    ) {
        showPreview = false
        if (!isInit) {
            cameraEngine =
                CameraFactory.createCameraEngine(type)
            cameraEngine?.run {
                if (!isInit) {
                    init(CameraConst.assetsPath, CameraConst.assetsPath)
                }

                openOrCloseCamera(true)
                setCameraRotateAndMirror(
                    CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_ALL,
                    angle!!,
                    true
                )
                setEventCallBack(object : LiveCheckListener {
                    override fun faceIsLive(data: ImageData) {
                        Log.e("null_check", "faceIsLive: ${msgCallBack}", )
                        msgCallBack?.invoke(0, "", data)
                    }

                    override fun changePose() {
                        Log.e("null_check", "faceIsLive: ${msgCallBack}", )
                        msgCallBack?.invoke(-1, "请对准屏幕", null)
                    }

                    override fun faceIsUnLive() {
                        Log.e("null_check", "faceIsLive: ${msgCallBack}", )
                        msgCallBack?.invoke(-2, "识别失败", null)
                    }
                })
                setCameraStreamFormat(
                    CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_ALL,
                    CameraEngineConfig.FormatStreamType.FORMAT_MJPEG,
                    640,
                    480,
                    30
                )
            }
        }
        cameraEngine?.setCameraDataListener(
            CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_VIS,
            CameraEngineConfig.FormatDataType.RGB888,
            true
        ) {
            it?.run {
                if (!showPreview) {
                    startDetect()
                    showPreview = true
                }
            }
        }?.apply {
            if (this) {
                isInit = true
            }
        }
    }

    fun release(){
        msgCallBack = null
        cameraEngine?.setCameraDataListener(
            CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_VIS,
            CameraEngineConfig.FormatDataType.RGB888,
            true, null
        )
    }

    fun initSurfaceView(surfaceView: GLFrameSurface?) {
        cameraEngine?.initSurfaceView(surfaceView)
    }

    fun openOrCloseCamera(open: Boolean) {
        if (open) {
            cameraEngine?.openCamera(CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_ALL)

        } else {
            cameraEngine?.closeCamera(CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_ALL)
        }
    }

    fun openOrCloseStream(open: Boolean) {
        cameraEngine?.openOrCloseStream(CameraEngineConfig.CameraType.CLOUD_WALK_OPEN_ALL, open)
//        if(open){
//            cameraEngine?.startDetect(CameraEngineConfig.DetectType.CLOUD_WALK_DEC_TYPE_LIVENESS_CONTINUE,60000)
//        }

    }

    fun startDetect() {
        cameraEngine?.startDetect(
            CameraEngineConfig.DetectType.CLOUD_WALK_DEC_TYPE_LIVENESS_CONTINUE,
            60000
        )
    }


    fun bindToLifecycle(lifecycleOwner: LifecycleOwner) {
        cameraEngine?.bindToLifecycle(lifecycleOwner)
    }

    suspend fun stopDetect() {
        cameraEngine?.stopDetect()
    }

}